import base from './base';
// import loadjs from 'loadjs';
import * as file from '../constants/fileConfig';
import allStore from '../stores';

export const controlAsyncLoad = () =>{
    if (!_.isEmpty(base.globalStore.commonParam)) {
        //此刻代表首屏渲染完成
        if (!window.REQ_LOAD_DURATION) {
            window.DISPATCH_DURATION = new Date().getTime() - window.API_LOAD_END;
            window.REQ_LOAD_DURATION = new Date().getTime() - window.REQ_PAGE_LOAD_START;
        }
        //非Chrome下等首屏只读渲染完，再加载全量组件库
        window.__hasLoadFullResource__ !== true && loadFullResource();
    }
    base.globalStore.triggerJSCompleteEvent();
}

//加载全量组件库等资源
export const loadFullResource = () =>{
    window.__hasLoadFullResource__ = true;
    const loadjs = window.loadjs;
    const { globalStore } = allStore;
    const loadFn = () =>{
        window.ASYNC_BEGIN = new Date().getTime();
        //加载全量组件库资源
        // const fileList = file.antdFiles.concat(file.ecComFiles, file.comMobxFiles, file.odocFiles);      //IE下async无法保证顺序
        loadjs(file.antdFiles, ()=>{
            loadjs(file.ecComFiles, ()=>{
                loadjs(file.comMobxFiles.concat(file.odocFiles), ()=>{
                    window.ASYNC_DURATION = new Date().getTime() - window.ASYNC_BEGIN;
                    //标识全量组件库加载完成，再加载其它依赖异步资源
                    globalStore.markJSComplete();
                    //加载等待队列中的异步模块资源
                    loadWaitingModule();
                    //加载依赖的文件列表
                    loadjs(file.dependFiles, 'depend');
                    //触发一次图片懒加载
                    window.formImgLazyLoad(jQuery('.wf-req-form-scroll'));
                });
            });
        });
    }
    //可根据url参数控制异步加载延时时间，调试用
    let delayLoadTime = 0;
    const { urlParams } = globalStore;
    if(!!urlParams.delayAsyncLoad){
        delayLoadTime = isNaN(parseInt(urlParams.delayAsyncLoad)) ? 999999999 : parseInt(urlParams.delayAsyncLoad);
    }else{
        if(globalStore.browserVersion !== "Chrome")
            delayLoadTime = 300;
    }
    delayLoadTime > 0 ? window.setTimeout(loadFn, delayLoadTime) : loadFn();
}

const waitingModule = {};    //异步模块资源需要等到全量组件库加载完毕才加载

export const asyncLoadModule = (modulename, files=[], callback, async=true) =>{
    const loadjs = window.loadjs;
    const { JSComplete } = base.globalStore;
    if(JSComplete){     //组件资源已加载完成，loadjs直接发请求
        loadjs.isDefined(modulename) ? loadjs.ready(modulename, callback) : loadjs(files, modulename, {
            async: async,
            success: callback
        });
    }else{  //组件资源未加载完成，插入等待队列，组件资源加载完成后请求等待队列发请求
        let obj = modulename in waitingModule ? waitingModule[modulename] : {files, async, callbacks:[]};
        obj.callbacks.push(callback);
        waitingModule[modulename] = obj;
    }
}

const loadWaitingModule = () =>{
    const loadjs = window.loadjs;
    for(const modulename in waitingModule){
        const moduleCfg = waitingModule[modulename];
        const files = moduleCfg.files || [];
        const async = "async" in moduleCfg ? moduleCfg.async : true;
        const callbacks = moduleCfg.callbacks || [];
        const loadComplateFun = () => {
            callbacks.map(fun =>{
                if(typeof fun === "function")
                    fun();
            });
        }
        loadjs.isDefined(modulename) ? loadjs.ready(modulename, loadComplateFun) : loadjs(files, modulename, {
            async: async,
            success: loadComplateFun
        });
    }
}